package io.aircraft.modules.wms.controller;

import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.exception.ErrorCode;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.wms.dto.OrderDTO;
import io.aircraft.modules.wms.service.OrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 出库主单
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-11-16
 */
@RestController
@RequestMapping("wms/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("page")
    @RequiresPermissions("wms:order:page")
    public Result<PageData<OrderDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<OrderDTO> page = orderService.page(params);

        return new Result<PageData<OrderDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:order:info")
    public Result<OrderDTO> get(@PathVariable("id") Long id) {
        OrderDTO data = orderService.get(id);

        return new Result<OrderDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:order:save")
    public Result save(@RequestBody OrderDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        //1.校验同站台是否已有任务
        OrderDTO record = orderService.getListByStation(dto.getStationNo());
        if (record != null) {
            return new Result().error("终点位置存在尚未完成的出库计划!请优先处理");
        }
        ProcResult procResult = orderService.releaseTask(dto);
        //2.处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("allot/{id}")
    @RequiresPermissions("wms:order:allot")
    public Result orderAllot(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id);
        //调用配货逻辑
        ProcResult procResult = orderService.orderAllot(id);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("refund/{id}")
    @RequiresPermissions("wms:order:refund")
    public Result orderRefund(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id);
        //调用配货取消逻辑
        ProcResult procResult = orderService.orderRefund(id);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PostMapping("audi/{id}")
    @RequiresPermissions("wms:order:audi")
    public Result orderAudi(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id);
        //调用出库执行逻辑
        ProcResult procResult = orderService.orderAudi(id);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:order:update")
    public Result update(@RequestBody OrderDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        orderService.update(dto);

        return new Result();
    }

    @PutMapping("{id}")
    @LogOperation("单据挂起")
    @RequiresPermissions("wms:order:hang-up")
    public Result hangUp(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id);
        final ProcResult procResult = orderService.orderHangUp(id);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @DeleteMapping("{id}")
    @RequiresPermissions("wms:order:delete")
    public Result delete(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id);

        //调用单据取消逻辑
        ProcResult procResult = orderService.orderCancel(id);

        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }
}